<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="被官方搞垮的收集器">
<meta property="og:type" content="article">
<meta property="og:title" content="深入理解JVM - Shenadoah收集器">
<meta property="og:url" content="https://whitestore.top/2022/02/20/shenadoahshoujiqi/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="被官方搞垮的收集器">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813095845.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813100222.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813101905.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813102622.png">
<meta property="article:published_time" content="2022-02-20T12:40:57.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.268Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="shenadoah">
<meta property="article:tag" content="难读">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813095845.png">

<link rel="canonical" href="https://whitestore.top/2022/02/20/shenadoahshoujiqi/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>深入理解JVM - Shenadoah收集器 | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2022/02/20/shenadoahshoujiqi/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          深入理解JVM - Shenadoah收集器
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-02-20 20:40:57" itemprop="dateCreated datePublished" datetime="2022-02-20T20:40:57+08:00">2022-02-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JVM/" itemprop="url" rel="index"><span itemprop="name">JVM</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/02/20/shenadoahshoujiqi/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/02/20/shenadoahshoujiqi/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>
            <div class="post-description">被官方搞垮的收集器</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="深入理解JVM-Shenadoah收集器"><a href="#深入理解JVM-Shenadoah收集器" class="headerlink" title="深入理解JVM - Shenadoah收集器"></a>深入理解JVM - Shenadoah收集器</h1><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>​    zgc和shenadoah的收集器是面向未来的收集器，目前还处于不断完善的阶段，虽然我们平时可能不太用的上，但是了解和基本掌握它是必须的，关于这一块网上的内容确实比较少，所以个人还是使用了书本里面的内容进行总结。</p>
<p>​    另外这两个垃圾收集器是完全舍弃分代这个概念的，注意是完全舍弃，并不是类似G1收集器虽然使用了分区但是本质上还是分代收集的收集器。</p>
<p>​    由于这两个收集器的内容较多，这里分开进行讲解，本篇讲解Shenadoah收集器。</p>
<h1 id="思维导图："><a href="#思维导图：" class="headerlink" title="思维导图："></a>思维导图：</h1><p>不想看文字的，可以查看思维导图：<span class="exturl" data-url="aHR0cHM6Ly93d3cubXVidWNtLmNvbS9kb2MvN0w0Vy1GQTBDRkI=" title="https://www.mubucm.com/doc/7L4W-FA0CFB">https://www.mubucm.com/doc/7L4W-FA0CFB<i class="fa fa-external-link"></i></span></p>
<a id="more"></a>



<h1 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h1><h1 id="低延迟垃圾收集器"><a href="#低延迟垃圾收集器" class="headerlink" title="低延迟垃圾收集器"></a>低延迟垃圾收集器</h1><p>​    在正式介绍之前，有必要说明一下整个背景，现代的垃圾收集器考虑的点主要为下面这三个条件：<strong>内存占用，吞吐量，延迟</strong>，通过之前的收集器介绍，我们知道了虽然主流的g1收集器在标记阶段实现了并发，但是在初始标记和筛选回收阶段还是需要进行阶段性的stop world的，这个垃圾收集器并没有做到真正意义上的并发，并且由于分区+region分代的设计限制，必然会产生垃圾收集的停顿。所以未来的垃圾收集器主要目标将会是面向极低延迟进军，也就是努力实现用户线程和垃圾收集器线程的完全并发运行。</p>
<p>​    值得一提的是虽然新生的低延迟垃圾收集器抛弃了分代的概念，但是G1的Region分块以及垃圾停顿模型保留了下来，我们也可以看到几乎所有的垃圾收集器都是基于前人的努力成果进行改进，所以不需要十分恐惧内容很难或者是完全颠覆想法。</p>
<h2 id="g1和cms实现并发的细节"><a href="#g1和cms实现并发的细节" class="headerlink" title="g1和cms实现并发的细节"></a>g1和cms实现并发的细节</h2><p>​    之前的文章提到了<strong>增量更新</strong>和<strong>原始快照</strong>，cms使用的是增量更新，g1使用的是原始快照，另外cms使用标记-清除的算法，免不了内存碎片，而g1虽然使用标记-整理，但是终究还是需要进行暂停的，所以这是一个非常棘手的问题。</p>
<p>​    </p>
<h1 id="Shenadoah"><a href="#Shenadoah" class="headerlink" title="Shenadoah"></a>Shenadoah</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>​    这款收集器是首款非jdk官方开发的垃圾收集器，由redhat公司开发，后续被捐赠给eclipse基金会，目前由eclipse基金会进行维护和管理。虽然Shenadoah从设计的细节来看有很多需要完善的地方，但是确实已经具备了独立作为垃圾收集器使用的条件。</p>
<p>​    比较可惜的是oracle因为商业竞争的问题会把shenandoah通过条件编译的手段进行排除使用比较麻烦，所以shenadoah只能存在于<strong>openJDK</strong>无法在OracleJdk上进行部署，但是这款垃圾收集器依然值得我们学习。</p>
<h2 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h2><p>​    下面来说一下shenadoah的特点：</p>
<p><strong>Region</strong></p>
<p>​    和G1收集器的设计原理一样使用的是region进行分块，同样有着大对象的概念，默认的策略也是根据算法回收最有价值的region。</p>
<p><strong>没有分代和连接矩阵</strong></p>
<p>​    注意是没有分代的概念，默认不使用分代收集，换言之就是没有新生代和老年代的说法。那要怎么设计？Shenandoah的解决方案是使用独立构建的“连接矩阵”全局数据结构来维护region的引用关系，也不要被连接矩阵这种名词给吓到了，其实本质上是一个<strong>二维数组</strong>结构，比如我们在<strong>Region N</strong>引用了<strong>Region M</strong>，那么就会在对应的N行M列上打上一个标记，也就是说全局的对象引用都会通过这个表来维护，这也意味着连接矩阵会随着对象的增长不断膨胀。</p>
<blockquote>
<p>G1收集器是放弃固定分代而是使用分区的设计，然而分区本质上还是分代的，只不过可以自由决定属于哪一个分代。</p>
</blockquote>
<p>​    下面直接从书里面拷了一张图来显示连接矩阵的设计：</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813095845.png" alt=""></p>
<blockquote>
<p>不得不说的是这个连接矩阵在设计上是仁者见仁智者见智了，维护一个矩阵虽然很方便但是随着对象的增多会呈现出指数性的表膨胀，这样来看还是一个值得商榷的设计，这一点在后续的垃圾收集器zgc介绍中会提到，zgc发现了连接矩阵的问题，采用了一些改进手段来解决表膨胀的问题。</p>
</blockquote>
<p><strong>支持并发收集和整理</strong></p>
<p>​    支持并发收集和整理，可以实现标记和整理阶段完全和用户线程并发执行。</p>
<h2 id="算法细节"><a href="#算法细节" class="headerlink" title="算法细节"></a>算法细节</h2><p>​    那么这个收集器是如何做到这些事情的呢，在介绍工作流程之前，我们来聊一下算法的实现细节。</p>
<h3 id="brooks-pointer"><a href="#brooks-pointer" class="headerlink" title="brooks pointer"></a>brooks pointer</h3><p>​    历史原因不过多介绍，这里说明一下这个值的含义：<strong>转发指针</strong>。转发指针是什么呢？它是用来解决对象移动和用户程序并发的一种解决方案。</p>
<p>​    Brooks pointer的工作原理：就是在对象的结构布局上增加一个新的引用字段，这个引用<strong>通常情况下指向自己</strong>，当对象发生转移的时候，brooks pointer会指向<strong>新引用的地址</strong>，这样指向旧引用的对象就可以修复引用指向新对象，这种结构在形式上和JVM的句柄定位类似，都是使用一种间接的访问形式，差别是转发指针会分散存在对象头内部。（之前我们讨论过对象头是动态扩展的格式）</p>
<p>​    这种设计形式也有点类似于链表的设计形式。</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813100222.png" alt=""></p>
<blockquote>
<p>补充：<strong>之前如何解决对象引用问题？</strong></p>
<p>​    使用的是一种在原有的对象内存之上<strong>设置保护陷阱+异常处理</strong>的方式，一旦出现访问旧对象的行为，就会进入到保护陷阱当中，并且进入异常处理器进行代码逻辑和引用的修复。这种方式看起来十分的有效，但是如果没有操作系统的支持，就需要通过不断的<strong>用户态到内核态的切换</strong>，需要耗费更多的上下文切换资源，也是一种非常耗费性能的妥协办法。</p>
</blockquote>
<p><strong>缺点</strong>：</p>
<p>​    虽然转发指针被优化到只有一行汇编指令的程度，但是依然要消耗对象访问的效率，当然这个方案毫无疑问是比内存陷阱要好，</p>
<p><strong>并发问题</strong>：</p>
<p>​    转发指针的设计意味着他必然有并发的问题，如果发生并发操作，就需要保证写操作必须是在新复制的对象下面，不妨考虑下面的问题：</p>
<ol>
<li>收集器线程复制了新的对象副本</li>
<li>用户线程更新对象的某个字段</li>
<li>收集器线程更新转发指针的引用值为新副本地址。</li>
</ol>
<p>​    如果不防范这三个问题，就会导致用户线程的对象变更都是操作旧对象，所以必须针对指针的访问操作采取同步的措施。解决办法和对象的引用分配方式也是类似的也是使用<strong>CAS+更新失败重试的操作机制</strong>。</p>
<p>​    最后，还需要注意的是Shenadoah必须使用读写屏障去维护brooks pointer（并发问题决定了要时刻保持同步），这个代价是非常大的。下面我们接着来讲讲读写屏障的问题。</p>
<h3 id="读写屏障"><a href="#读写屏障" class="headerlink" title="读写屏障"></a>读写屏障</h3><p>​    shenandoah不仅使用了写屏障还使用了读屏障，读屏障也是类似对象引用操作的一个AOP的切面，我们都知道对象的读操作肯定是要多于写操作的，所以使用读屏障的代价要大很多。</p>
<blockquote>
<p>写屏障的概念可以看专栏之前的文章：<span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uY24vcG9zdC82OTk0NjQyMjQ2ODQyNzEyMTAwI2hlYWRpbmctMTg=" title="https://juejin.cn/post/6994642246842712100#heading-18">深入理解JVM - Hotspot算法细节#写屏障<i class="fa fa-external-link"></i></span> </p>
</blockquote>
<p>​    当然Shenandoah开发者也意识到这个问题，在<strong>JDK13的</strong>版本中，改用了基于“引用访问屏障”的方式解决读屏障的问题，“引用访问屏障”指的是只拦截对象类似是引用类型的数据进行访问屏障的拦截，这样就可以省去一些原生类型并发修改访问的操作，减少庞大的读屏障维护开销。</p>
<blockquote>
<p>从这里也可以看出来Redhat的开发团队在设计jvm垃圾收集器上的经验缺乏，但是可以及时调整解决问题。</p>
</blockquote>
<h2 id="工作过程："><a href="#工作过程：" class="headerlink" title="工作过程："></a>工作过程：</h2><p>​    shenandoah的工作步骤可以划分为9个步骤，最新版本的shenandoah还在初始标记的步骤前面增加了三个步骤，简单理解为分代收集当中的Minor GC操作即可。</p>
<p>接下来说一下具体的步骤：</p>
<ol>
<li>初始标记：和G1一样，首先标记出所有的GC ROOT关联的对象，<strong>注意这个阶段是需要停顿的</strong>。</li>
<li><strong>并发标记</strong>：和G1一样，根据GC ROOT遍历对象图，标记出所有的可达对象，这个阶段<strong>和用户线程一起并发</strong>。</li>
<li>最终标记：还是和G1一样，处理剩下的对象扫描操作，同时计算出回收价值最高的Region，最终标记阶段有<strong>一小段的暂停</strong>。</li>
<li>并发清理：这个阶段用于清理整个区域一个存活对象都没有的Region，这个阶段是<strong>并发执行</strong>的。</li>
<li><strong>并发回收</strong>：<strong>核心差异点，</strong>在这个阶段，会把回收集里面存活对象先复制一份到到其他未使用的Region，但是要注意这个操作并不是同步的而是和用户线程并发的，再次强调是并发的，不是和G1的交替的暂停和运行的工作方式，注意这里的实现原理就是之前说的“Brooks Pointer”，同时使用<strong>转发指针的操作+cas锁</strong>将旧对象的引用修复为新对象的方式。这个阶段也是和G1最大的区别，实现了垃圾回收和用户线程的并发操作。</li>
<li>初始引用更新：并发回收阶段复制对象结束之后，还需要把堆中的所有指向旧对象的更新到复制之后的新地址，这个操作也叫做<strong>引用更新</strong>。同样会产生一个<strong>非常短暂的停顿</strong>。</li>
<li><strong>并发引用更新：</strong>真正开始引用更新的操作，时间长短取决于引用的多少。毫无疑问也是并发执行的</li>
<li>最终引用更新：解决堆中引用更新之后，修正GC ROOT引用，这个阶段是最后一次停顿，停顿时间和GC Roots的数量有关。</li>
<li>并发清理：最后回收没有任何对象的空Region</li>
</ol>
<blockquote>
<p>并发标记、并发回收、并发引用更新这三个阶段是最重要的，重点记忆即可。</p>
</blockquote>
<p>下面的图是从官方的wiki扒过来的：</p>
<blockquote>
<p>Init Mark：初始标记</p>
<p>Final Mark：最终标记</p>
<p>Init-UR：初始引用更新</p>
<p>Final-UR：最终引用更新</p>
</blockquote>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813101905.png" alt="并发标记、并发回收、并发引用"></p>
<h2 id="结果对比"><a href="#结果对比" class="headerlink" title="结果对比"></a>结果对比</h2><p>​    官方有一张对比图来显示Shenandoah的垃圾收集耗时对比，从图中可以看到做到了几乎无延迟的垃圾收集：</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210813102622.png" alt=""></p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>​    Shenadoah收集器是收款非JDK官方开发的收集器，然而很遗憾的是，因为商业竞争关系，他只存在于OpenJDK，没有被商用，并且后续由于更加ZGC的开发，Shenadoah的作用也在逐渐减少，但是不得不承认的作为没有JVM垃圾收集器开发经验的开发者们开发的收集器，这款收集器满足了要求并且十分值得借鉴和学习。</p>
<p>​    另外可以看到即使是简化工作原理，现代的垃圾收集器也已经十分复杂了，由于目前大部分开发者还是使用JDK8和G1等垃圾收集器，所以这些垃圾收集器在目前看来还是属于面向未来的收集器，但是毫无疑问我们需要不断的学习。</p>
<h1 id="其他资料："><a href="#其他资料：" class="headerlink" title="其他资料："></a>其他资料：</h1><p><span class="exturl" data-url="aHR0cHM6Ly93aWtpLm9wZW5qZGsuamF2YS5uZXQvZGlzcGxheS9zaGVuYW5kb2FoL01haW4=" title="https://wiki.openjdk.java.net/display/shenandoah/Main">Shenadoah垃圾收集器官网介绍<i class="fa fa-external-link"></i></span></p>
<blockquote>
<p>Shenandoah收集器的JVM参数案例：<code>java -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=passive -Xlog:gc</code></p>
</blockquote>
<h1 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h1><p>​    这本书讲述这款垃圾收集器的内容算是比较粗浅，但是对于我们了解这款收集器来说算是足够了。想要了解更多内容，个人建议直接找上面提供的官方WIKI入手，毕竟开发出来的人对这个东西才是最了解的。</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>阿东
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2022/02/20/shenadoahshoujiqi/" title="深入理解JVM - Shenadoah收集器">https://whitestore.top/2022/02/20/shenadoahshoujiqi/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JVM/" rel="tag"># JVM</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/02/20/riqiapi/" rel="prev" title="【Java】JAVA8 实战 - 日期API">
      <i class="fa fa-chevron-left"></i> 【Java】JAVA8 实战 - 日期API
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/02/20/jvmzgc/" rel="next" title="深入理解JVM - ZGC收集器">
      深入理解JVM - ZGC收集器 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#深入理解JVM-Shenadoah收集器"><span class="nav-number">1.</span> <span class="nav-text">深入理解JVM - Shenadoah收集器</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">2.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#思维导图："><span class="nav-number">3.</span> <span class="nav-text">思维导图：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#概述"><span class="nav-number">4.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#低延迟垃圾收集器"><span class="nav-number">5.</span> <span class="nav-text">低延迟垃圾收集器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#g1和cms实现并发的细节"><span class="nav-number">5.1.</span> <span class="nav-text">g1和cms实现并发的细节</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Shenadoah"><span class="nav-number">6.</span> <span class="nav-text">Shenadoah</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">6.1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#特点"><span class="nav-number">6.2.</span> <span class="nav-text">特点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#算法细节"><span class="nav-number">6.3.</span> <span class="nav-text">算法细节</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#brooks-pointer"><span class="nav-number">6.3.1.</span> <span class="nav-text">brooks pointer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#读写屏障"><span class="nav-number">6.3.2.</span> <span class="nav-text">读写屏障</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#工作过程："><span class="nav-number">6.4.</span> <span class="nav-text">工作过程：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结果对比"><span class="nav-number">6.5.</span> <span class="nav-text">结果对比</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">7.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#其他资料："><span class="nav-number">8.</span> <span class="nav-text">其他资料：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#写在最后"><span class="nav-number">9.</span> <span class="nav-text">写在最后</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
